//
//  TFChangeLanguage.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/22.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFChangeLanguage: NSObject {
   @objc public static let sharedInstance = TFChangeLanguage()
    
    var isLoad:Bool = false
    private override init() {}
    
    override func copy() -> Any {
        return self // SingletonClass.shared
    }
    
    override func mutableCopy() -> Any {
        return self // SingletonClass.shared
    }
    
    
    let  AppLanguage:NSString = "appLanguage2";
    

    //初始化多语言功能
    @objc func initLanguage()
    {
        var languageCode = "zh-Hans"
       
        let AppleLanguages = UserDefaults.standard.object(forKey: "AppleLanguages")
        if AppleLanguages is NSArray  {
            let AppleLanguagesArray:NSArray = AppleLanguages as! NSArray
            if (AppleLanguagesArray.count>0)  {
                 let str = AppleLanguagesArray[0]
                
                if (str is NSString ) {
                    if (str as AnyObject).description.hasPrefix("en") {
                        languageCode = "en"
                    }
                    
                    
                }
            }
            
        }
       //设置语言
        self.setLanguage(language: languageCode)
    }
    @objc public func setLanguage(language:String)
    {
        UserDefaults.standard.set(language, forKey: "appLanguage2")

        UserDefaults.standard.synchronize()
        
    }
    //获取当前语言
   @objc public func  currentLanguage() ->NSString
   {
       if TFChangeLanguage.sharedInstance.isLoad == false {
           TFChangeLanguage.sharedInstance.isLoad = true
           TFChangeLanguage.sharedInstance.initLanguage()
       }
       let language:NSString = UserDefaults.standard.object(forKey: "appLanguage2") as! NSString
       
        return language;
    }
    @objc public static func ZBLocalized(key:NSString)->NSString
    {
    
        if TFChangeLanguage.sharedInstance.isLoad == false {
            TFChangeLanguage.sharedInstance.isLoad = true
            TFChangeLanguage.sharedInstance.initLanguage()
        }
        
        let AppLanguage = "appLanguage2"

        let obj = NSString.init(format: "%@", UserDefaults.standard.object(forKey: AppLanguage) as! CVarArg)

        let path = Bundle.main.path(forResource: obj as String, ofType: "lproj")! as String

        
        let name =  Bundle.init(path: path)?.localizedString(forKey: key as String, value: "", table: "TalkfunEnglish")

        
        return name! as NSString
    }
    @objc public static func localized(_ key:NSString)->String
    {
    
        if TFChangeLanguage.sharedInstance.isLoad == false {
            TFChangeLanguage.sharedInstance.isLoad = true
            TFChangeLanguage.sharedInstance.initLanguage()
        }
        
        let AppLanguage = "appLanguage2"

        let obj = NSString.init(format: "%@", UserDefaults.standard.object(forKey: AppLanguage) as! CVarArg)

        let path = Bundle.main.path(forResource: obj as String, ofType: "lproj")! as String

        
        let name =  Bundle.init(path: path)?.localizedString(forKey: key as String, value: "", table: "TalkfunEnglish")

        
        return name! as String
    }

//
//    + (NSString*)ZBLocalized:(NSString*)key
//    {
//
//        NSString * pathForResource = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:AppLanguage]];
//
//        NSString *path = [[NSBundle mainBundle] pathForResource:pathForResource ofType:@"lproj"];
//
//        NSString *name = [[NSBundle bundleWithPath:path] localizedStringForKey:key value:@"" table:@"TalkfunEnglish"];
//
//        return name;
//    }
}
